package auction.chapter16.test;

import org.dbunit.operation.DatabaseOperation;
import org.hibernate.StatelessSession;
import org.testng.annotations.Test;

import auction.chapter16.dao.hibernate.ItemDAOHibernate;
import auction.model.Item;

public class PersistentStateTransitions extends HibernateIntegrationTest {

	protected void prepareSettings() {
		dataSetLocation="auction/chapter16/test/basedata.xml";
		beforeTestOperations.add(DatabaseOperation.CLEAN_INSERT);
	}
	
	@Test(groups="integration-hibernate")
	public void storeAndLoadItem(){
		//Start a unit of work
		sessionFactory.getCurrentSession().beginTransaction();
		
		//Prepare the DAOs
		ItemDAOHibernate itemDAO=new ItemDAOHibernate();
		itemDAO.setSession(sessionFactory.getCurrentSession());
		
		Item newi=new Item();
		newi.setTitle("my");
		itemDAO.makePersistent(newi);
		
		//End the unit of work
		sessionFactory.getCurrentSession().getTransaction().commit();
		
		//Direct SQL query for database state in auto-commit code
		StatelessSession s=sessionFactory.openStatelessSession();
		String t2=(String)s.createQuery("select title from Item where title='my'").uniqueResult();
		
		s.close();
		
		//Assert correctness of state
		assert t2.equals("my");
	}

}
